﻿IF EXISTS (SELECT * from sys.procedures p left join sys.schemas s on p.schema_id = s.schema_id where p.name='contact_select_all' and s.name='Crm')
DROP PROCEDURE [Crm].contact_select_all
GO
CREATE PROCEDURE [Crm].contact_select_all 
AS
BEGIN
SELECT 	
	[Contacts].[id_contact] AS [PK],
	[Contacts].[id_account] AS [AccountID],
	[Contacts].[title] AS [Title],
	[Contacts].[name] AS [Name],
	[Contacts].[given_name] AS [GivenName],
	[Contacts].[function] AS [Function],
	[Contacts].[phone1] AS [Phone1],
	[Contacts].[phone2] AS [Phone2],
	[Contacts].[fax] AS [Fax],
	[Contacts].[mail] AS [Mail],
	[Contacts].[address] AS [Address],
	[Contacts].[zip_code] AS [ZipCode],
	[Contacts].[city] AS [City],
	[Contacts].[country] AS [Country],
	[Contacts].[use_account_infos] AS [UseAccountInfos],
	[Meta].[References].[last_update] AS [LastUpdate],
	[Meta].[References].[Google] AS [Google],
	[Meta].[References].[Google_last_update] AS [GoogleLastUpdate],
	[Meta].[References].[ZohoCRM] AS [ZohoCRM],
	[Meta].[References].[ZohoCRM_last_update] AS [ZohoCRMLastUpdate],
	[Meta].[References].[from] AS [From],
	[Meta].[References].[Validated] AS [Validated]
FROM 
	[Contacts] 
    INNER JOIN [Meta].[References] ON  [Contacts].[id_contact] = [References].[id_reference] 
END